var util = require('../../../utils/util.js');
var wxcache = require('../../../utils/wxcache.js');
var user = require('../../../services/user.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
  data:{
    orderList: [],
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    orderListType:"ALL",//所要搜索的表单类型
    key_all: null,
    key_waitPays: null,
    key_waitPay:null,
    key_paying:null,
    key_failed:null,
    key_paySuccess:null,
    key_canceled:null,
    key_waitComment:null,
    key_finished:null,

  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    // 接收从个人中心页面传来的参数
    this.setData({
      currentTab: options.tab
    });
   
    if (options.tab == 1) {
      this.setData({
        orderListType: "WAIT_PAY"
      });
    }
    if (options.tab == 2) {
      this.setData({
        orderListType: "PAY_SUCCESS"
      });
    }
    if (options.tab == 3) {
      this.setData({
        orderListType: "WAIT_COMMENT"
      });
    }
    if (options.tab == 4){
      this.setData({
        orderListType: "FINISHED"
      });
    }

    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    this.getOrderList();
    this.getOrderStatusTab();
    
    
  },

  getOrderStatusTab: function () {
    util.request(api.OrderStatus).then(function (res) {
      console.log("订单状态tab返回值")
      console.log(res)
    })
  },

  /**
    * 订单列表页接口
    * userId : 用户id 必须
    * status ： 订单状态 ，不传为全部，可选值（WAIT_PAY，PAYING, PAY_SUCCESS, CANCELED, FAILED， WAIT_COMMENT）
    * startIndex ： 分页起始页 1 非必须
    * pageSize ： 分页每页展示数 20 非必须
   */
  getOrderList: function () {
    var that = this;
    user.checkLogin().then(function(res){
      
    },function(error){
      wx.showModal({
        title: '提示',
        content: '您还未登录，是否登录',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.navigateTo({
              url: '/pages/auth/registerAndLogin/registerAndLogin',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      }) 
    })
     
    var uid = wxcache.get("userInfo").id;
    var data = {}
    //订单状态
    data.status = that.data.orderListType;
    //分页起始页
    data.startIndex = 1 
    //分页每页展示数
    data.pageSize=100
    data.userId = uid
    data.bizType = 1
    util.request(api.OrderList, data).then(function (res) {
      console.log("订单列表返回值")
      console.log(res);
      if (that.data.orderListType == "ALL"){
        that.setData({
          key_all: res.responseBody.orderDetailList,
        });
      } else if (that.data.orderListType == "WAIT_PAY"){
        that.setData({
          key_waitPay: res.responseBody.orderDetailList,
        });
      } else if (that.data.orderListType == "PAYING") {
        that.setData({
          key_paying: res.responseBody.orderDetailList,
        });
      } else if (that.data.orderListType == "FAILED") {
        that.setData({
          key_failed: res.responseBody.orderDetailList,
        });
      }else if (that.data.orderListType == "PAY_SUCCESS"){
        that.setData({
          key_paySuccess: res.responseBody.orderDetailList,
        });
      } else if (that.data.orderListType == "CANCELED"){
        that.setData({
          key_canceled: res.responseBody.orderDetailList,
        });
      } else if (that.data.orderListType == "WAIT_COMMENT"){
        that.setData({
          key_waitComment: res.responseBody.orderDetailList,
        });
      } else if (that.data.orderListType == "FINISHED") {
        that.setData({
          key_finished: res.responseBody.orderDetailList,
        });
      }
    })

    var key_waitPays = [];

    if (this.data.key_waitPay != null && this.data.key_waitPay.length > 0) {
      key_waitPays.push(this.data.key_waitPay);
    }
    if (this.data.key_paying != null && this.data.key_paying.length > 0) {
      key_waitPays.push(this.data.key_paying);
    }
    if (this.data.key_failed != null && this.data.key_failed.length > 0) {
      key_waitPays.push(this.data.key_failed);
    }

    this.setData({
      key_waitPays: key_waitPays
    });
    console.log(this.data.key_waitPays);

  },

  //跳转详情界面
  queryItemClick: function (e) {
    var that = this;
    var viewId = e.currentTarget.id;
    var viewDataSet = e.currentTarget.dataset;
    var viewText = viewDataSet.text;
    var queryBean = "";
    if (viewText == "order_all"){
      //将对象转为string
      queryBean = JSON.stringify(that.data.key_all[viewId]);
    } else if (viewText == "order_daizhifu") {
      queryBean = JSON.stringify(that.data.key_waitPay[viewId]);
    } else if (viewText == "order_yizhifu") {
      queryBean = JSON.stringify(that.data.key_paySuccess[viewId]);
    } else if (viewText == "order_yiquxiao") {
      queryBean = JSON.stringify(that.data.key_canceled[viewId]);
    } else if (viewText == "order_daipingjia") {
      queryBean = JSON.stringify(that.data.key_waitComment[viewId]);
    } else if (viewText == "order_yiwancheng") {
      queryBean = JSON.stringify(that.data.key_finished[viewId]);
    }
    wx.navigateTo({
      url: '/pages/ucenter/orderDetail/orderDetail?queryBean=' + queryBean,
    })
  },

  payOrder(){
    wx.redirectTo({
      url: '/pages/pay/pay',
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    this.getOrderList();
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  
  //-----------------------------------------------
 
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
    //获取对应的内容
    if (this.data.currentTab == "0") {
      this.setData({
        orderListType: "ALL"
      })
    } else if (this.data.currentTab == "1") {
      this.setData({
        orderListType: "WAIT_PAY"
      })
    } else if (this.data.currentTab == "2") {
      this.setData({
        orderListType: "PAY_SUCCESS"
      })
    } else if (this.data.currentTab == "3") {
      this.setData({
        orderListType: "WAIT_COMMENT"
      })
    } else if (this.data.currentTab == "4") {
      this.setData({
        orderListType: "FINISHED"
      })
    }
    this.getOrderList();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
    }
    //获取对应的内容
    if(cur=="0"){
      this.setData({
        orderListType:"ALL"
      })
    }else if(cur=="1"){
      this.setData({
        orderListType: "WAIT_PAY"
      })
    }else if(cur=="2") {
      this.setData({
        orderListType: "PAY_SUCCESS"
      })
    }else if(cur=="3") {
      this.setData({
         orderListType: "WAIT_COMMENT"
      })
    } else if (cur == "4") {
      this.setData({
        orderListType: "FINISHED"
      })
    }
    this.getOrderList();
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  }


})